/*
 * @Author: sean.kei@163.com
 * @Date: 2021-06-08 15:07:55
 * @LastEditors: sean.kei@163.com
 * @LastEditTime: 2021-08-27 11:09:29
 */
package com.wxhandle.cleandemo.sso.handler;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nimbusds.jose.shaded.json.JSONObject;

import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

@Component
public class PerAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response,
            AccessDeniedException accessDeniedException) throws IOException, ServletException {
           //登录成功返回
           Map<String, Object> paramMap = new HashMap<>();
           paramMap.put("code", "503");
           paramMap.put("message", accessDeniedException.getMessage());
           //设置返回请求头
           response.setContentType("application/json;charset=utf-8");
           //写出流
           PrintWriter out = response.getWriter();
           out.write(JSONObject.toJSONString(paramMap));
           out.flush();
           out.close();
    }
    
}
